﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace LanLanKan
{
    public partial class Form1 : Form
    {
        static int N = 10;
        static int M = 10;
        //定义存放N*N个按钮对象的数组
        PictureBox[,] btns = new PictureBox[N, N];
        Image[] images = new Image[M];
        int totalTime = 120;
        //定义存放N*N空间位置状态的数组 0-空闲 1-M物体种类序号
        int[,] pos_status = new int[N, N];
        int clickFlag = 0;//点击的次序
        int row1, col1, row2, col2;
        //定义帮助和重新排列可用次数
        int helpmax = 2;
        int repalymax = 1;
        public Form1()
        {
            InitializeComponent();

        }

        private void createButton()
        {
            //动态生成N*N个按钮放置在屏幕上
            for (int i = 0; i < N; i++)
                for (int j = 0; j < N; j++)
                {
                    //1.创建一个Button对象
                    PictureBox obj = new PictureBox();
                    //2.设置相关属性
                    obj.Width = 60;  //宽度
                    obj.Height = 60; //高度               
                    obj.Location = new Point(60 * j + 60, 60 * i + 60);  //坐标位置
                    //3.将按钮对象放置屏幕上--加入到当前窗口的Controls中
                    this.Controls.Add(obj);
                    //4.将按钮对象加入到数组btns中进行保存
                    btns[i, j] = obj;
                    //5.设置按钮对象被点击后，调用的处理方法
                    btns[i, j].Click += new System.EventHandler(ButtonClickHandler);
                }//end for
        }
        //载入所有类型的图片
        private void LoadImages()
        {
            for (int j = 0; j < M; j++)
            {
                images[j] = Image.FromFile("images\\" + (j + 1) + ".png");
            }
        }

        //设置按钮上显示的图片
        private void showButton()
        {
            Random rd = new Random();
            /*for (int i = 0; i < N; i++)
                for (int j = 0; j < N; j++)
                {
                    int index = rd.Next(M);
                    btns[i, j].Image = images[index];
                }*/
            
            //1.确定每种物体类型的个数
            int[] numberPerType = new int[M];
            //1.1求绝对平均数
            int avg = N * N / M;
            //1.2判断是否是奇数
            if (avg % 2 > 0) avg--;
            //1.3给M中物体  每种分配avg个
            for (int i = 0; i < M; i++)
                numberPerType[i] = avg;
            //1.4分配剩下的零头
            int left = N * N - M * avg;
            int nGroup = left / 2;
            for (int i = 0; i < nGroup; i++)
                numberPerType[i] += 2;
            //2.将每种物体安排在N*N个空间上
            for (int i = 0; i < M; i++)
            {
                //取第i个物体的个数
                int n = numberPerType[i];
                for (int j = 0; j < n; j++)
                {
                    int row = rd.Next(N);
                    int col = rd.Next(N);
                    //判断该位置是否之前已经放置了某种物体
                    if (pos_status[row, col] == 0)
                    {
                        //将该位置上的图片设置为第i种物体
                        btns[row, col].Image = images[i];
                        pos_status[row, col] = i + 1;
                        btns[row, col].BackColor = Color.White;
                    }
                    else
                    {
                        j--;
                    }

                }
            }
            
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            LoadImages();
            createButton();
        }

        //点击开始按钮
        private void btnStart_Click(object sender, EventArgs e)
        {
            showButton();//设置不同位置显示的图片
            timer1.Enabled = true;
        }

        private void btnHelp_Click(object sender, EventArgs e)
        {
            helpmax--;
            if (helpmax == 0) btnHelp.Enabled = false;
            //寻找一对可以消除的位置，提示给用户
            //从左到右 从上到下 依次以每个有物体的位置为参照
            //寻找后面是否存在一个符合消除的条件的位置
            for (int i = 0; i < N; i++)
            {
                for (int j = 0; j < N; j++)
                {
                    if (pos_status[i, j] == 0)
                        continue;
                    //(i,j)位置上有物体，则判断后面是否存在
                    for (int row = i; row < N; row++)
                    {
                        //设置搜索列的起点
                        int col = row == i ? j + 1 : 0;
                        for (; col < N; col++)
                        {
                            //同种物体
                            if (pos_status[row, col] == pos_status[i, j])
                            {
                                //判断是否存在通路
                                if (llk.findRouter(pos_status, i, j, row, col))
                                {
                                    btns[i, j].BackColor = Color.Red;
                                    btns[row, col].BackColor = Color.Red;
                                    return;//返回
                                }
                            }
                        }
                    }
                }
            }
        }

        //重新排列
        private void btnReLay_Click(object sender, EventArgs e)
        {
            repalymax--;
            if (repalymax == 0) btnReLay.Enabled = false;
            //1.统计程序当前剩下的每种物体的个数
            int[] numberPerType = new int[M];
            for (int i = 0; i < N; i++)
            {
                for (int j = 0; j < N; j++)
                {
                    int status = pos_status[i, j];
                    if (status > 0)
                    {
                        numberPerType[status - 1]++;
                    }
                }
            }
            //2.清除格子上的数据
            for (int i = 0; i < N; i++)
            {
                for (int j = 0; j < N; j++)
                {
                    pos_status[i, j] = 0;
                    btns[i, j].Visible = false;
                }
            }
            //3.根据当前每种物体的个数，重新排列位置
            Random rd = new Random();
            for (int i = 0; i < M; i++)
            {
                int n = numberPerType[i];
                for (int j = 1; j < n; j++)
                {
                    int row = rd.Next(N);
                    int col = rd.Next(N);
                    if (pos_status[row, col] == 0)
                    {
                        pos_status[row, col] = i + 1;
                        btns[row, col].Image = images[i];
                        btns[row, col].Visible = true;
                        btns[row, col].BackColor = Color.White;
                    }
                    else j--;
                }
            }
        }

        private void timer1_Tick_1(object sender, EventArgs e)
        {
            totalTime--;
            progressBar1.Value = totalTime;
            if (totalTime <= 0)
            {
                timer1.Enabled = false;
            }
        }

        //定义用户点击矩阵按钮时的处理方法
        private void ButtonClickHandler(object sender, EventArgs e)
        {
            //找出本次被点击的按钮的位置序号
            PictureBox current = (PictureBox)sender;
            for (int i = 0; i < N; i++)
                for (int j = 0; j < N; j++)
                {
                    if (btns[i, j] == current)
                    {
                        clickFlag++;//点击次序+1
                        if (clickFlag == 1)
                        {
                            row1 = i;
                            col1 = j;
                        }
                        else
                        {
                            row2 = i;
                            col2 = j;
                            //进行消除的处理
                            doDisappear();
                            clickFlag = 0;
                        }
                        return;
                    }
                }//end for j i
            //btnStart.Enabled = false;
        }

        private void doDisappear()
        {
            //判断（row1,col1)(row2,col2)位置上的物体是否可消除
            //1.判断是否是相同的物体
            if (pos_status[row1, col1] != pos_status[row2, col2])
            {
                return;
            }
            if (row1 == row2 && col1 == col2) 
            {
                return;
            }
            //2.判断是否存在路径
            if(llk.findRouter(pos_status, row1, col1, row2, col2))
            { 
                //3.进行消除
                btns[row1, col1].Visible = false;
                pos_status[row1, col1] = 0;
                btns[row2, col2].Visible = false;
                pos_status[row2, col2] = 0;
                
                //4.判断是否全部消除结束
                if (llk.checkWinner(pos_status))
                {
                    timer1.Enabled = false;
                    MessageBox.Show("赢了呀···");
                }
            }            
        }     
    }
}
